﻿// ------------------------------------------------------------------------------------
//      Copyright (c) uhavemyword@gmail.com. All rights reserved.
//      Created by Ben at 2013/2/7 12:36:41
// ------------------------------------------------------------------------------------

namespace CP.NLayer.Client.WpfClient.Common
{
    using System;
    using System.ComponentModel;

    /// <summary>
    /// ViewModel for telerik:RadBusyIndicator Control
    /// </summary>
    public class RadBusyModel : ViewModelBase
    {
        private bool _isBusy = false;
        private bool _isIndeterminate = true;
        private string _busyContent = "Loading...";
        private TimeSpan _displayAfter = new TimeSpan(5000000); //half second
        private Action _onCompleted;

        public TimeSpan DisplayAfter
        {
            get { return this._displayAfter; }
            set
            {
                if (this._displayAfter != value)
                {
                    this._displayAfter = value;
                    this.RaisePropertyChanged(() => this.DisplayAfter);
                }
            }
        }

        public bool IsIndeterminate
        {
            get { return this._isIndeterminate; }
            set
            {
                if (this._isIndeterminate != value)
                {
                    this._isIndeterminate = value;
                    this.RaisePropertyChanged(() => this.IsIndeterminate);
                }
            }
        }

        public bool IsBusy
        {
            get { return this._isBusy; }
            set
            {
                if (this._isBusy != value)
                {
                    this._isBusy = value;
                    this.RaisePropertyChanged(() => this.IsBusy);
                }
            }
        }

        public string BusyContent
        {
            get { return this._busyContent; }
            set
            {
                if (this._busyContent != value)
                {
                    this._busyContent = value;
                    this.RaisePropertyChanged(() => this.BusyContent);
                }
            }
        }

        public void DoWork(Action action, Action onCompleted = null)
        {
            this.IsBusy = true;
            this._onCompleted = onCompleted;
            var backgroundWorker = new BackgroundWorker();
            backgroundWorker.DoWork += this.OnBackgroundWorkerDoWork;
            backgroundWorker.RunWorkerCompleted += OnBackgroundWorkerRunWorkerCompleted;
            backgroundWorker.RunWorkerAsync(action);
        }

        private void OnBackgroundWorkerRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            var backgroundWorker = sender as BackgroundWorker;
            backgroundWorker.DoWork -= this.OnBackgroundWorkerDoWork;
            backgroundWorker.RunWorkerCompleted -= this.OnBackgroundWorkerRunWorkerCompleted;
            this.IsBusy = false;
            if (_onCompleted != null)
            {
                this._onCompleted();
            }
        }

        private void OnBackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
        {
            Action action = e.Argument as Action;
            action();
        }
    }
}
